﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace log4net
{

    public interface ILog
    {
        void Debug(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null);
        void Error(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null);
        void Info(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null);
        void Warn(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null);
    }
    public  class Log : ILog
    {
        static Log defaultLog;
     public static Log Default() {
            if (defaultLog==null)
            {
                defaultLog = new Log();
                defaultLog.Name = "default";
            }
            return defaultLog;
        }
        public string Name { get; set; }
        //StrParse strParse = new StrParse();
        public readonly string Path = "wwwroot/log4n.log";
        void WriteLog(string level,object msg, object msg2 = null, object msg3 = null, string callName = null) {
            if (msg!=null&&!(msg is string ||msg is Exception))
            {
                msg = JsonConvert.SerializeObject(msg);
            }
            if (msg2 != null && !(msg2 is string || msg2 is Exception))
            {
                msg2 = JsonConvert.SerializeObject(msg2);
            }
            if (msg3 != null && !(msg3 is string || msg3 is Exception))
            {
                msg3 = JsonConvert.SerializeObject(msg3);
            }
            var str = $"{DateTime.Now } {Name}-4n:{level}:{callName}:{msg},{msg2},{msg3} \r\n";
            if (level != "Debug")
            {
                Console.Write(str);
            }   
            lock (Path)
            {
               // File.AppendAllText(Path, str);
            }
           
        }
        //CallerFilePath,CallerLineNumber
        public void Debug(object  msg,object msg2=null,object msg3=null,[CallerMemberName] string callName=null) {
            WriteLog("Debug", msg, msg2, msg3, callName);
        }
        public void Info(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) {
            WriteLog("Info", msg, msg2, msg3, callName);
        }
        public void Warn(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) {
            WriteLog("Warn", msg, msg2, msg3, callName);
        }
        public void Error(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) {
            WriteLog("Error", msg, msg2, msg3, callName);
        }

        public void d(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) { Debug(msg,msg2,msg3,callName); }
        public void i(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) { Info(msg, msg2, msg3, callName); }
        public void w(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) { Warn(msg, msg2, msg3, callName); }
        public void e(object msg, object msg2 = null, object msg3 = null, [CallerMemberName] string callName = null) { Error(msg, msg2, msg3, callName); }
    }
    public class LogManager
    {
        public static Log GetLogger(object name=null)
        {
            return Log.Default();
        }
    }

}